package com.zhuxulin.midi.event.meta;

import java.io.IOException;
import java.io.OutputStream;

import com.zhuxulin.midi.event.MidiEvent;
import com.zhuxulin.midi.util.MidiUtil;
import com.zhuxulin.midi.util.ReadVarLen;

public class GenericMetaEvent extends MetaEvent {

	private byte[] mData;
	private int type;
	protected GenericMetaEvent(long tick, long delta, int type,	ReadVarLen length, byte[] data) {
		super(tick, delta, type, length);
		
		mData = data;
		this.type=type&0xFF;
	}

	@Override
	protected int getEventSize() {
		return 1 + 1 + mLength.getByteCount() + mLength.getValue();
	}
	
	@Override
	protected void writeToFile(OutputStream out) throws IOException {
		super.writeToFile(out);
		out.write(mLength.getBytes());
		out.write(mData);
	}
	@Override
	public byte[] getCmd(){
		cmd=MidiUtil.addByte(new byte[]{(byte) 0xFF,(byte) type}, mLength.getBytes());
		cmd=MidiUtil.addByte(cmd, mData);
//				new byte[]{(byte) 0xFF,(byte) type,mLength.getBytes(),mData};
		return cmd;
	}
	@Override
	public int compareTo(MidiEvent other) {
		
		if(mTick != other.getTick()) {
			return mTick < other.getTick() ? -1 : 1;
		}
		if(mDelta.getValue() != other.getDelta()) {
			return mDelta.getValue() < other.getDelta() ? 1 : -1;
		}
		
		return 1;
	}

	
}
